package cn.edu.zafu.bigdata.x.hjn;

import java.io.*;
import java.util.*;

class Student implements Serializable {
    private String id;      // 学号
    private String name;    // 姓名
    private String phone;   // 电话

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getPhone() {
        return phone;
    }

    @Override
    public String toString() {
        return "学号: " + id + ", 姓名: " + name + ", 电话: " + phone;
    }
}

public class StudentManagementSystem {
    private static final String FILE_PATH = "students.dat";
    private static List<Student> students = new ArrayList<>();

    public static void main(String[] args) {
        loadStudents();
        Scanner scanner = new Scanner(System.in);
        while (true) {
            displayMenu();
            int choice = scanner.nextInt();
            scanner.nextLine();  // Consume the newline character

            switch (choice) {
                case 1:
                    addStudent(scanner);
                    break;
                case 2:
                    searchStudent(scanner);
                    break;
                case 3:
                    updateStudent(scanner);
                    break;
                case 4:
                    deleteStudent(scanner);
                    break;
                case 5:
                    saveStudents();
                    System.out.println("程序退出，所有数据已保存。");
                    return;
                default:
                    System.out.println("无效的输入，请重新选择。");
            }
        }
    }

    private static void displayMenu() {
        System.out.println("\n======= 学生信息管理系统 =======");
        System.out.println("1. 添加学生信息");
        System.out.println("2. 检索学生信息");
        System.out.println("3. 更新学生信息");
        System.out.println("4. 删除学生信息");
        System.out.println("5. 退出程序");
        System.out.print("请输入操作编号 (1-5): ");
    }

    private static void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_PATH))) {
            students = (List<Student>) ois.readObject();
        } catch (FileNotFoundException e) {
            System.out.println("没有找到学生数据文件，初始化为空数据。");
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("加载学生数据时出错：" + e.getMessage());
        }
    }

    private static void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_PATH))) {
            oos.writeObject(students);
        } catch (IOException e) {
            System.out.println("保存学生数据时出错：" + e.getMessage());
        }
    }

    private static void addStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (findStudentById(id) != null) {
            System.out.println("学号重复，请重新输入。");
            return;
        }

        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();

        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        students.add(new Student(id, name, phone));
        System.out.println("学生信息已添加。");
    }

    private static void searchStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入要检索的学号 (输入 'QUIT' 退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            Student student = findStudentById(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private static void updateStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入要更新的学号 (输入 'QUIT' 退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            Student student = findStudentById(id);
            if (student == null) {
                System.out.println("未找到该学生。");
                continue;
            }

            System.out.print("请输入新的姓名: ");
            String name = scanner.nextLine();

            System.out.print("请输入新的电话: ");
            String phone = scanner.nextLine();

            student = new Student(id, name, phone);
            System.out.println("学生信息已更新。");
        }
    }

    private static void deleteStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入要删除的学号 (输入 'QUIT' 退出): ");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("QUIT")) {
                break;
            }

            Student student = findStudentById(id);
            if (student == null) {
                System.out.println("未找到该学生。");
                continue;
            }

            students.remove(student);
            System.out.println("学生信息已删除。");
        }
    }

    private static Student findStudentById(String id) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                return student;
            }
        }
        return null;
    }
}
